//
//  ZAlreadyViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/3/28.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZAlreadyViewController.h"
#import "ZMyAskAlreadyView.h"
#import "ZLookFriendDeatilViewController.h"
#import "ZReplyDetailedViewController.h"
#import "ZValuationViewController.h"
#import "ZOnceSubmitViewController.h"
#import "ZConsultTableViewCell.h"
#import "ZConsultModel.h"
#import "SPVoiceManager.h"
#import "ZNodoctorViewController.h"
#import "SPPlaceholderCell.h"
static NSString *const placeCellID = @"placeholders";
static NSString * const KZNewMyAskAlreadyIdentifier = @"KZNewMyAskAlreadyIdentifier";
static int alrPage = 1;
@interface ZAlreadyViewController ()<UITableViewDataSource,UITableViewDelegate>
//@property (nonatomic,strong)ZMyAskAlreadyView * AskAlreadyView; // 已回复
@property (nonatomic,strong) UITableView * askTabView;//
@property (nonatomic,strong) NSMutableArray <ZConsultModel *> * cModelArray;
@property (nonatomic,strong) LZUserInfo * userInfo;
@property (nonatomic,assign) BOOL isHaveData;
@end

@implementation ZAlreadyViewController

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"%s",__func__);
    [super viewWillAppear:animated];
    if ([[SPVoiceManager defaultManager] isPlaying]) {
        [[SPVoiceManager defaultManager] stop];
    }
    
}
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _cModelArray = [@[] mutableCopy];
    _userInfo = [LZUserInfo getUserInfo];
    self.isHaveData = YES;
    [self.view addSubview:self.askTabView];
    
    [self.askTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    

    [self.askTabView.mj_header beginRefreshing];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh1:) name:@"refresh1" object:nil];
}
- (void)refresh1:(NSNotification *)info
{
    
    [self.askTabView.mj_header beginRefreshing];
}
// 表格视图协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.isHaveData?self.cModelArray.count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHaveData) {
        ZConsultTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:KZNewMyAskAlreadyIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.cModel = self.cModelArray[indexPath.row];
        return cell;
    }else {
        SPPlaceholderCell *cell = [tableView dequeueReusableCellWithIdentifier:placeCellID];
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHaveData == NO) {
        return;
    }
    ZConsultModel * model = self.cModelArray[indexPath.row];
    if ([model.status isEqualToString:@"1"]) {
        ZOnceSubmitViewController * ZOSVC =[[ZOnceSubmitViewController alloc] init];
//        ZOSVC.oPersonModel = p;
        ZOSVC.qId = model.qId;
        ZOSVC.ishiden = YES;
        [self.navigationController pushViewController:ZOSVC animated:YES];
    }else if ([model.status isEqualToString:@"2"]){
        ZReplyDetailedViewController * ZRDVC = [[ZReplyDetailedViewController alloc] init];
        ZRDVC.qId = model.qId;
        ZRDVC.ishiden = YES;
//        ZRDVC.personModel = pmodel;
        [self.navigationController pushViewController:ZRDVC animated:YES];
    }else if ([model.status isEqualToString:@"3"]) {
        ZValuationViewController * zvVC = [[ZValuationViewController alloc] init];
        zvVC.qId = model.qId;
        zvVC.ishiden = YES;
//        zvVC.personModel = pmodel;
        [self.navigationController pushViewController:zvVC animated:YES];
    }else if ([model.status isEqualToString:@"4"]){
        ZLookFriendDeatilViewController * zlVC = [[ZLookFriendDeatilViewController alloc] init];
        zlVC.qId = model.qId;
        zlVC.ishiden = YES;
        [self.navigationController pushViewController:zlVC animated:YES];
    }else {
        ZNodoctorViewController * noDVc = [[ZNodoctorViewController alloc] init];
        noDVc.qId = model.qId;
        noDVc.ishiden = YES;
        [self.navigationController pushViewController:noDVc animated:YES];
    }

}
- (void)alreadRePlyNetWorking {
    alrPage = 1;
    self.isHaveData = YES;
    NSDictionary * wDict = @{@"uid":_userInfo.uid,@"type":@"2",@"p":[NSString stringWithFormat:@"%d",alrPage]};
    [YJProgressHUD showProgress:@"刷新中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:wDict paramarsSite:@"Consult/index" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [self alreadHandleDataWithArray:dict[@"list"]];
        }else {
            self.isHaveData = NO;
            [self.askTabView reloadData];
            [YJProgressHUD showMessage:@"刷新失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        self.isHaveData = NO;
        [self.askTabView reloadData];
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];
}

- (void)alreadMoreRePlyNetWorking {
    alrPage++;
    NSDictionary * wDict = @{@"uid":_userInfo.uid,@"type":@"2",@"p":[NSString stringWithFormat:@"%d",alrPage]};
    [YJProgressHUD showProgress:@"刷新中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:wDict paramarsSite:@"Consult/index" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [self alreadMoreHandleDataWithArray:dict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"刷新失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];

}
- (void)alreadHandleDataWithArray:(NSArray *)array {
    [self.askTabView.mj_footer resetNoMoreData];
    [self.cModelArray removeAllObjects];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        self.isHaveData = NO;
        [self.askTabView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [self.askTabView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        ZConsultModel * model = [[ZConsultModel alloc] initWithDict:dict];
        [self.cModelArray addObject:model];
    }
    [self.askTabView reloadData];
}
- (void)alreadMoreHandleDataWithArray:(NSArray *)array {
    [self.askTabView.mj_footer resetNoMoreData];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [self.askTabView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [self.askTabView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        ZConsultModel * model = [[ZConsultModel alloc] initWithDict:dict];
        [self.cModelArray addObject:model];
    }
    [self.askTabView reloadData];
}


- (UITableView *)askTabView {
    if (_askTabView) {
        return _askTabView;
    }
    
    _askTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _askTabView.dataSource = self;
    _askTabView.delegate = self;
    _askTabView.rowHeight = 160*ysIZE;
    _askTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
     [_askTabView registerNib:[UINib nibWithNibName:NSStringFromClass([SPPlaceholderCell class]) bundle:nil] forCellReuseIdentifier:placeCellID];
    [_askTabView registerClass:[ZConsultTableViewCell class] forCellReuseIdentifier: KZNewMyAskAlreadyIdentifier];
    _askTabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self alreadRePlyNetWorking];
        [_askTabView.mj_header endRefreshing];
    }];
    _askTabView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self alreadMoreRePlyNetWorking];
        [_askTabView.mj_footer endRefreshing];
    }];
    return _askTabView;

}
@end
